package com.boe.csb.core.common.utils;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.SimplifiedObjectMeta;
import com.boe.csb.core.common.exception.CSBErrorCode;
import com.boe.csb.core.common.exception.CSBException;
import org.apache.commons.io.IOUtils;
import org.apache.http.conn.ssl.TrustStrategy;

import java.io.*;

/**
 * OssTool
 * Created by tianjin.lp on 16/9/13.
 */
public class OssTool {

    public final static String ACCESS_ID;
    public final static String ACCESS_KEY;
    public final static String ENDPOINT;
    public final static String DEFAULT_BUCKET;
    public final static String URL;
    public static OSSClient ossClient;
    TrustStrategy trustStrategy;

    static {
        ACCESS_ID = PropertyUtils.getProperty("oss.client.accessid");
        ACCESS_KEY = PropertyUtils.getProperty("oss.client.accesskey");
        ENDPOINT = PropertyUtils.getProperty("oss.client.endpoint");
        DEFAULT_BUCKET = PropertyUtils.getProperty("oss.client.default.bucket");
        URL = PropertyUtils.getProperty("oss.client.url");
        ossClient = new OSSClient(ENDPOINT, ACCESS_ID, ACCESS_KEY);
    }

    public static void uploadToOss(String fileKey, File localFile) {
        uploadToOss(DEFAULT_BUCKET, fileKey, localFile);
    }

    public static void uploadToOss(String bucketName, String fileKey, File localFile) {
        ossClient.putObject(bucketName, fileKey, localFile);
    }

    public static void uploadToOss(String fileKey, InputStream inputStream) {
        uploadToOss(DEFAULT_BUCKET, fileKey, inputStream);
    }

    public static void uploadToOss(String bucketName, String fileKey, InputStream inputStream) {
        ossClient.putObject(bucketName, fileKey, inputStream);
    }

    public static void uploadToOss(String bucketName, String fileKey, byte[] data) {
        ossClient.putObject(bucketName, fileKey, new ByteArrayInputStream(data));
    }

    public static void uploadToOss(String fileKey, byte[] data) {
        ossClient.putObject(DEFAULT_BUCKET, fileKey, new ByteArrayInputStream(data));
    }

    public static Long getOssFileLength(String fileKey) {
        // 获取文件的部分元信息
        SimplifiedObjectMeta objectMeta = ossClient.getSimplifiedObjectMeta(DEFAULT_BUCKET, fileKey);
        return objectMeta.getSize();
    }

    // 下载文件
    public static byte[] downloadFromOSS(String key) {
        return downloadFromOSS(DEFAULT_BUCKET, key);
    }

    // 下载文件
    public static byte[] downloadFromOSS(String bucketName, String key) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        downloadFromOSS(bucketName, key, outputStream);
        try {
            return outputStream.toByteArray();
        } finally {
            IOUtils.closeQuietly(outputStream);
        }
    }

    public static void downloadFromOSS(String key, OutputStream outputStream) {
        downloadFromOSS(DEFAULT_BUCKET, key, outputStream);
    }

    public static void downloadFromOSS(String bucketName, String key, OutputStream outputStream) {
        OSSObject object = ossClient.getObject(bucketName, key);
        InputStream input = object.getObjectContent();
        try {
            byte[] buffer = new byte[4096];
            int n;
            while (-1 != (n = input.read(buffer))) {
                outputStream.write(buffer, 0, n);
            }
        } catch (IOException e) {
            throw new CSBException(CSBErrorCode.UNKNOWN_ERROR, "DOWNLOAD ERROR", e);
        } finally {
            //函数外传来的outputStream不应该在函数内关闭
            IOUtils.closeQuietly(input);
        }
    }

    // 获取文件内容输入流
    public static InputStream getOssContentInputStream(String bucketName, String key) {
        OSSObject object;
        try {
            object = ossClient.getObject(bucketName, key);
            // 获取Object的输入流
            return object.getObjectContent();
        } catch (Exception e) {
            throw new CSBException(CSBErrorCode.UNKNOWN_ERROR, "getOssContentInputStream ERROR", e);
        }
    }

    //删除文件
    public static void deleteFile(String key) {
        deleteFile(DEFAULT_BUCKET, key);
    }

    //删除文件
    public static void deleteFile(String bucketName, String key) {
        try {
            ossClient.deleteObject(bucketName, key);
        } catch (Exception e) {
            throw new CSBException(CSBErrorCode.UNKNOWN_ERROR, "deleteFile ERROR", e);
        }
    }

    public static void changeInputToOutput(InputStream fileStream, OutputStream out) throws IOException {
        int read;
        byte[] bytes = new byte[2048];
        while ((read = fileStream.read(bytes)) > 0) {
            out.write(bytes, 0, read);
        }
        out.flush();
    }

    public static String getURL(String objName) {
        return URL + "/" + objName;
    }
}
